package com.chenyun.satoken.starter.handler;

import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.strategy.SaStrategy;
import com.chenyun.satoken.starter.annotation.SaCheckAdmin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.AnnotatedElement;
import java.util.function.Consumer;

/**
 * 校验 SaCheckAdmin 注解权限
 * 判断是否具有admin权限
 * @author: Lonni
 * @date: 2022/11/21 0021 14:44
 */
@Service
@Slf4j
public class CheakAdminSaAnnotationHandler implements SaAnnotationHandler {

    public CheakAdminSaAnnotationHandler(){
        log.info("进去SaCheckAdmin注解校验类!");
    }

    /**
     * 具体的校验方法
     * 权限不满足时请抛出 SaTokenException 异常
     * @return void
     * @see SaTokenException
     */
    @Override
    public Consumer<AnnotatedElement> checkElementAnnotation() {
        return (target) -> {
            // 自定义的校验
            SaCheckAdmin checkAdmin = (SaCheckAdmin) SaStrategy.me.getAnnotation.apply(target, SaCheckAdmin.class);
            if (checkAdmin != null) {
                //todo  校验 SaCheckAdmin 注解权限
//                boolean isadmin = SaLoginUtil.isAdmin();
//                if (!isadmin) {
//                    throw new SaTokenException("用户不具有admin权限");
//                }
            }
        };
    }

}



